package com.yy.permissions_general.module.system.schedule.controller;

import com.yy.permissions_general.common.aopSubmit.aop.NoRepeatSubmit;
import com.yy.permissions_general.module.system.aopLog.aop.MyLog;
import com.yy.permissions_general.module.system.schedule.entity.ScheduleJob;
import com.yy.permissions_general.module.system.schedule.service.ScheduleJobService;
import com.yy.permissions_general.utils.ResultUtil;
import com.yy.permissions_general.utils.SequenceUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 定时任务前端控制器
 * </p>
 *
 * @author YY
 * @since 2022-02-09
 */
@RestController
@RequestMapping("/scheduleJob")
public class ScheduleJobController {

    @Resource
    private ScheduleJobService scheduleJobService;

    /**
    * 查询列表
    * @param scheduleJob
    * @param limit
    * @param page
    * @return
    */
    @MyLog("查询列表")
    @NoRepeatSubmit(key="scheduleJob.list")
    @RequestMapping("/list")
    public ResultUtil list(ScheduleJob scheduleJob, @RequestParam(value = "limit",defaultValue = "999999")int limit, @RequestParam(value = "page",defaultValue = "0")int page) {
      return ResultUtil.buildPagination(scheduleJobService.selectPageList(scheduleJob,limit,page));
    }

    /**
    * 新增数据
    * @param scheduleJob
    * @return
    */
    @MyLog("新增数据")
    @NoRepeatSubmit(key="scheduleJob.addScheduleJob")
    @PreAuthorize("hasPermission('/scheduleJob/addScheduleJob','scheduleJob:addScheduleJob')")
    @RequestMapping("/addScheduleJob")
    public ResultUtil addScheduleJob(ScheduleJob scheduleJob) {
        scheduleJob.setJobId(SequenceUtil.makeStringId());
        scheduleJobService.addScheduleJob(scheduleJob);
        return ResultUtil.success();
    }

    /**
     * 执行一次定时任务
     * @param jobId
     * @return
     */
    @MyLog("执行一次定时任务")
    @NoRepeatSubmit(key="scheduleJob.submitScheduleJob")
    @RequestMapping("/submitScheduleJob")
    @PreAuthorize("hasPermission('/scheduleJob/submitScheduleJob','scheduleJob:submitScheduleJob')")
    public ResultUtil submitScheduleJob(String jobId) {
        scheduleJobService.submitScheduleJob(jobId);
        return ResultUtil.success();
    }

    /**
     * 更新定时任务数据
     * @param scheduleJob
     * @return
     */
    @MyLog("更新定时任务数据")
    @NoRepeatSubmit(key="scheduleJob.updateScheduleJob")
    @RequestMapping("/updateScheduleJob")
    @PreAuthorize("hasPermission('/scheduleJob/updateScheduleJob','scheduleJob:updateScheduleJob')")
    public ResultUtil updateScheduleJob( ScheduleJob scheduleJob) {
        scheduleJobService.updateScheduleJob(scheduleJob);
        return ResultUtil.success();
    }

    /**
     * 停止定时任务
     * @param jobId
     * @return
     */
    @MyLog("停止定时任务")
    @NoRepeatSubmit(key="scheduleJob.stopScheduleJob")
    @RequestMapping("/stopScheduleJob")
    @PreAuthorize("hasPermission('/scheduleJob/stopScheduleJob','scheduleJob:stopScheduleJob')")
    public ResultUtil stopScheduleJob(String jobId) {
        scheduleJobService.stopScheduleJob(jobId);
        return ResultUtil.success();
    }

    /**
     * 恢复定时任务
     * @param jobId
     * @return
     */
    @MyLog("恢复定时任务")
    @RequestMapping("/resetScheduleJob")
    @NoRepeatSubmit(key="scheduleJob.resetScheduleJob")
    @PreAuthorize("hasPermission('/scheduleJob/resetScheduleJob','scheduleJob:resetScheduleJob')")
    public ResultUtil resetScheduleJob(String jobId) {
        scheduleJobService.resetScheduleJob(jobId);
        return ResultUtil.success();
    }

    /**
    * 根据ID删除记录
    * @param jobId
    * @return
    */
    @MyLog("删除")
    @NoRepeatSubmit(key="scheduleJob.delete")
    @PreAuthorize("hasPermission('/scheduleJob/delete','scheduleJob:delete')")
    @RequestMapping("/delete")
    public ResultUtil delete(String jobId) {
        scheduleJobService.deleteById(jobId);
        return ResultUtil.success();
    }

    /**
    * 批量删除数据
    * @param ids ID集合
    * @return
    */
    @MyLog("批量删除")
    @NoRepeatSubmit(key="scheduleJob.deleteBatch")
    @PreAuthorize("hasPermission('/scheduleJob/deleteBatch','scheduleJob:deleteBatch')")
    @RequestMapping("/deleteBatch")
    public ResultUtil deleteBatch(@RequestParam(value = "ids[]",required = true) List<Long> ids) {
        scheduleJobService.deleteBatch(ids);
        return ResultUtil.success();
    }

}
